#include "Catalog.h"
#include "StarEntry.h"

void Catalog::setDatafileName(QString fname)
{
  datafile.setFileName(fname); 
}

void Catalog::setEpoch(double e)
{
 epoch = e; 
}

void Catalog::loadData()
{
 return; 
}

std::vector<StarEntry *> *Catalog::getStars()
{
  return &stars;
}

std::set< StarEntry *, classstarcomp> *Catalog::getStarSet()
{
  if (starset.empty()) {
   for (std::vector<StarEntry *>::iterator it=stars.begin(); it < stars.end(); it++)
     starset.insert(*it);
  }
  return &starset;
}


int Catalog::rowCount(const QModelIndex& parent) const
{
  //printf("Row count %d\n", stars.size());
  return stars.size();
}

int Catalog::columnCount(const QModelIndex& parent) const
{
  //printf("Column count\n");
  return 6;
}

QVariant Catalog::data(const QModelIndex& index, int role) const
{ 
  //printf("data %d %d %d %g\n", role, index.row(), index.column(), stars.at(index.row())->raJ2000);
  if(role == Qt::DisplayRole)
    {
      char sexa[13]; sexa[12]='\0';
      //printf("data %d %d %d %g\n", role, index.row(), index.column(), stars.at(index.row())->raJ2000);
      switch (index.column()) {
	case 0: return QString::number(stars.at(index.row())->indexcatalog);
	case 1: return (prefix ? (*prefix +QString(" ")) : QString("")) + QString::number(stars.at(index.row())->numidcatalog);	
	case 2: format_sexa(sexa, stars.at(index.row())->raJ2000, 2, 360000); return QString(sexa);
	case 3: format_sexa(sexa, stars.at(index.row())->decJ2000, 3, 360000); return QString(sexa);
	//case 3: return QString::number(stars.at(index.row())->raJNOW);
	//case 4: return QString::number(stars.at(index.row())->decJNOW);
	case 4: return QString::number(stars.at(index.row())->vmag);
	case 5: return QString(stars.at(index.row())->name);
    }
  }
  return QVariant::Invalid;
}

QVariant Catalog::headerData(int section, Qt::Orientation orientation, int role) const
{
  //printf("headerData\n");
  if(role == Qt::DisplayRole)
  {
    if(orientation == Qt::Horizontal)
      switch (section) {
	case 0: return QString("Index");
	case 1: return QString("IDcatalog");
	case 2: return QString("RA(J2000)");
	case 3: return QString("DEC(J2000)");
	//case 3: return QString("RA(JNOW)");
	//case 4: return QString("DEC(JNOW)");
	case 4: return QString("V.Mag");
	case 5: return QString("Name");
    }
    }
  return QVariant::Invalid;
}


int Catalog::format_sexa(char *out, double a, int w, int fracbase)
{
	char *out0 = out;
	unsigned long n;
	int d;
	int f;
	int m;
	int s;
	int isneg;

	/* save whether it's negative but do all the rest with a positive */
	isneg = (a < 0);
	if (isneg)
	    a = -a;

	/* convert to an integral number of whole portions */
	n = (unsigned long)(a * fracbase + 0.5);
	d = n/fracbase;
	f = n%fracbase;
	/* form the whole part; "negative 0" is a special case */
	if (isneg && d == 0)
	    out += sprintf (out, "%*s-0", w-2, "");
	else
	    out += sprintf (out, "%*d", w, isneg ? -d : d);

	/* do the rest */
	switch (fracbase) {
	case 60:	/* dd:mm */
	    m = f/(fracbase/60);
	    out += sprintf (out, ":%02d", m);
	    break;
	case 600:	/* dd:mm.m */
	    out += sprintf (out, ":%02d.%1d", f/10, f%10);
	    break;
	case 3600:	/* dd:mm:ss */
	    m = f/(fracbase/60);
	    s = f%(fracbase/60);
	    out += sprintf (out, ":%02d:%02d", m, s);
	    break;
	case 36000:	/* dd:mm:ss.s*/
	    m = f/(fracbase/60);
	    s = f%(fracbase/60);
	    out += sprintf (out, ":%02d:%02d.%1d", m, s/10, s%10);
	    break;
	case 360000:	/* dd:mm:ss.ss */
	    m = f/(fracbase/60);
	    s = f%(fracbase/60);
	    out += sprintf (out, ":%02d:%02d.%02d", m, s/100, s%100);
	    break;
	default:
	    printf ("fs_sexa: unknown fracbase: %d\n", fracbase);
            return -1;
	}

	return (out - out0);
}

